package com.charley.icloud.admin.webapps.sysmenu.service.impl;

import com.charley.icloud.admin.common.constant.SysTableConstants;
import com.charley.icloud.admin.webapps.sysmenu.dao.SysMenuMapper;
import com.charley.icloud.admin.webapps.sysmenu.pojo.entity.SysMenuDO;
import com.charley.icloud.admin.webapps.sysmenu.service.SysMenuService;
import com.charley.icloud.admin.webapps.sysuser.pojo.entity.SysUserDO;
import com.charley.icloud.common.constants.SystemNumberConstants;
import com.charley.icloud.datasource.page.MybatisPageHelper;
import com.charley.icloud.datasource.page.PageRequest;
import com.charley.icloud.datasource.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SysMenuServiceImpl implements SysMenuService {

    @Resource
    private SysMenuMapper sysMenuMapper;

    @Override
    public int save(SysMenuDO record) {
        if (record.getParentId() == null) {
            record.setParentId(0L);
        }
        // 新增
        if (record.getId() == null || SystemNumberConstants.SYSTEM_NUMBER_LONG_ZERO.equals(record.getId())) {
            record.setOrderNum(sysMenuMapper.findMenuNumberByParentId(record.getParentId()));
            record.setCreateBy("System");
            record.setCreateTime(new Date());
            return sysMenuMapper.insertSelective(record);
        }
        record.setLastUpdateBy("System");
        record.setLastUpdateTime(new Date());
        return sysMenuMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(SysMenuDO record) {
        return sysMenuMapper.deleteByPrimaryKey(record);
    }

    @Override
    public int delete(List<SysMenuDO> records) {
        for (SysMenuDO record : records) {
            record.setLastUpdateBy("System");
            record.setLastUpdateTime(new Date());
            delete(record);
        }
        return 1;
    }

    @Override
    public SysMenuDO findById(Long id) {
        return sysMenuMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult findPage(PageRequest pageRequest) {
        return MybatisPageHelper.findPage(pageRequest, sysMenuMapper);
    }

    @Override
    public List<SysMenuDO> findTree(String userId, Byte menuType) {
        List<SysMenuDO> sysMenus = new ArrayList<>();
        List<SysMenuDO> menus = findByUserId(userId);
        for (SysMenuDO menu : menus) {
            if (menu.getParentId() == null || menu.getParentId() == 0) {
                menu.setLevel(0);
                if (!exists(sysMenus, menu)) {
                    sysMenus.add(menu);
                }
            }
        }
        sysMenus.sort((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
        findChildren(sysMenus, menus, menuType);
        return sysMenus;
    }

    private List<SysMenuDO> findByUserId(String userId) {

        return null;
    }


    @Override
    public List<SysMenuDO> findByUser(String userName) {
        if (userName == null || "".equals(userName) || SysTableConstants.SYS_USER_TABLE_TYPE_SUPER_ADMIN.equals(userName)) {
            return sysMenuMapper.findAll();
        }
        return sysMenuMapper.findByUserName(userName);
    }

    @Override
    public List<SysMenuDO> findByUserAccount(SysUserDO sysUser) {

        if (sysUser.getRoleNames() == null || "".equals(sysUser.getRoleNames()) || sysUser.getRoleNames().contains("" + SysTableConstants.SYS_USER_TABLE_TYPE_SUPER_ADMIN)) {
            return sysMenuMapper.findAll();
        }
        return sysMenuMapper.findByAccount(sysUser.getAccount());
    }


    private void findChildren(List<SysMenuDO> SysMenus, List<SysMenuDO> menus, int menuType) {
        for (SysMenuDO SysMenu : SysMenus) {
            List<SysMenuDO> children = new ArrayList<>();
            for (SysMenuDO menu : menus) {
                if (menuType == 1 && menu.getType() == 2) {
                    // 如果是获取类型不需要按钮，且菜单类型是按钮的，直接过滤掉
                    continue;
                }
                if (SysMenu.getId() != null && SysMenu.getId().equals(menu.getParentId())) {
                    menu.setParentName(SysMenu.getName());
                    menu.setLevel(SysMenu.getLevel() + 1);
                    if (!exists(children, menu)) {
                        children.add(menu);
                    }
                }
            }
            SysMenu.setChildren(children);
            children.sort((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
            findChildren(children, menus, menuType);
        }
    }

    private boolean exists(List<SysMenuDO> sysMenus, SysMenuDO sysMenu) {
        boolean exist = false;
        for (SysMenuDO menu : sysMenus) {
            if (menu.getId().equals(sysMenu.getId())) {
                exist = true;
            }
        }
        return exist;
    }

}
